/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.absisgroup.nsc.utilities;

import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.MessageResourcesFactory;

/**
 *
 * @author Hue
 */
public class InitServlet extends HttpServlet {
    /** Creates a new instance of InitServlet */
    public InitServlet() {
    }
    
    @Override
    public void init(final ServletConfig config) throws ServletException {
        super.init(config);
        final ServletContext context = config.getServletContext();
        context.setAttribute("appVersion", config.getInitParameter("version"));
        initApplication(config);
        registerDriver(context);
    }
    
    private void initApplication(final ServletConfig config) throws ServletException {
	final String value = config.getInitParameter("application");
	if (value == null) {
            return;
        }
        final String factory = config.getInitParameter("factory");
	try {
            final ServletContext context = config.getServletContext();
            context.setAttribute("ApplicationName",config.getInitParameter("ApplicationName"));
            context.setAttribute("MOD_PK",config.getInitParameter("MOD_PK"));
            final String oldFactory = MessageResourcesFactory.getFactoryClass();
            if (factory != null) {
                MessageResourcesFactory.setFactoryClass(factory);
            }
            final MessageResourcesFactory factoryObject = MessageResourcesFactory.createFactory();
            final MessageResources application = factoryObject.createResources(value);
            MessageResourcesFactory.setFactoryClass(oldFactory);
            application.setReturnNull(false);
            context.setAttribute("org.apache.struts.action.MESSAGE", application);
	} 
        catch (Exception ex) {
            ex.printStackTrace();
	}
    }
    
    private void registerDriver(final ServletContext context) {
        try {
            InputStream in = context.getResourceAsStream("WEB-INF/db.properties");
            Properties p = new Properties();
            p.load(in);
            in.close();
            
            String driver = p.getProperty("driver");
            if(driver != null && !driver.isEmpty()) {
                Class.forName(driver).newInstance();
                context.setAttribute(Constants.MYSQL_URL, p.getProperty("url"));
                context.setAttribute(Constants.MYSQL_USER, p.getProperty("username"));
                context.setAttribute(Constants.MYSQL_PASSWORD, p.getProperty("password"));
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
